/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.ClinicalInformation;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.ClinicalInfoSyncBroker;
import systole.synchronization.remote.entities.ClinicalInfoRemote;

/**
 *
 * @author jmj
 */
public class ClinicalInfoSyncBrokerDB extends BrokerDB implements ClinicalInfoSyncBroker {

    public void saveClinicalInfoRemote(ClinicalInfoRemote clinicalInfoRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving remote clinical info");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(clinicalInfoRemote);
            this.logger.logDebug("remote clinical info saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save clinical info remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<ClinicalInfoRemote> getClinicalInfoToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<ClinicalInformation> getClinicalInfoToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting clinical info to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select ci from ClinicalInfoRemote cir right join cir.clinicalInformation ci where cir=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<ClinicalInformation> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get clinical info to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los informes clínicos", e.fillInStackTrace());
        }
    }

    public ClinicalInformation getClinicalInfoByRemoteId(int remoteId) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting clinical info by remote id");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    ClinicalInfoRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            ClinicalInfoRemote clinicalInfoRemote = (ClinicalInfoRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (clinicalInfoRemote != null ? clinicalInfoRemote.getClinicalInformation() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get clinical info by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los informes clínicos", e.fillInStackTrace());
        }
    }

    public ClinicalInfoRemote getClinicalInfoRemoteByClinicalInfo(ClinicalInformation clinicalInfo) throws ExceptionDAO {
         try {
            this.logger.logDebug("getting clinical info remote by local clinical info");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    ClinicalInfoRemote.class).add(Restrictions.eq("clinicalInformation", clinicalInfo)).setMaxResults(1);
            ClinicalInfoRemote sportRemote = (ClinicalInfoRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return sportRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote clinical info by clinical info, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los informes clínicos", e.fillInStackTrace());
        }
    }

    public int getLastRemoteClinicalInfoIdSynchronized() throws ExceptionDAO {
          try {
            this.logger.logDebug("getting last remote clinical info sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    ClinicalInfoRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            ClinicalInfoRemote clinicalInfoRemote = (ClinicalInfoRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (clinicalInfoRemote != null ? clinicalInfoRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote clinical info synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los informes clínicos", e.fillInStackTrace());
        }
    }
}
